package com.tarena.luban.sentinel;

import com.alibaba.csp.sentinel.slots.block.degrade.DegradeRule;
import com.alibaba.csp.sentinel.slots.block.degrade.DegradeRuleManager;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRule;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import java.util.ArrayList;
import java.util.List;

@SpringBootApplication
public class SentinelPractice {
    public static void main(String[] args) {
        SpringApplication.run(SentinelPractice.class,args);
        //定义一个限流规则.
        List<FlowRule> flowRules=new ArrayList<>();
        FlowRule flowRule=new FlowRule();
        //填写规则的具体属性
        //明确这个规则限制和检查的资源是谁
        flowRule.setResource("sayHi");
        //设置 流控方式 qps=1 并发=0
        flowRule.setGrade(1);
        //限流的阈值 count
        flowRule.setCount(1);
        flowRules.add(flowRule);
        //sentinel组件 加载定义的规则
        FlowRuleManager.loadRules(flowRules);
        //目前规则 定义的意思 : 访问sayHi qps超过阈值1 就会限制流量,不让访问
        List<DegradeRule> degradeRules=new ArrayList<>();
        DegradeRule degradeRule=new DegradeRule();
        degradeRule.setResource("sayHi");
        degradeRule.setCount(100);//慢调用的时间 1ms
        degradeRule.setGrade(0);//1 异常比例 2 异常数
        degradeRule.setMinRequestAmount(1);
        degradeRule.setSlowRatioThreshold(0.5);
        degradeRule.setTimeWindow(10);
        degradeRule.setStatIntervalMs(10000);
        degradeRules.add(degradeRule);
        DegradeRuleManager.loadRules(degradeRules);

    }
}
